package com.hundsun.yr.universal.library.view.hsdialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.TextView;

import com.hundsun.yr.universal.library.R;

public class HSIOSDialog implements OnClickListener {
	public final static int  LEFTBUTTON = 0;
	public final static int  RIGHTBUTTON = 1;
	public static final int  CLOSEBUTTON = 2;
	private LayoutInflater inflater;
	private Context context;

	private TextView tvMsg;
	private CharSequence message;
	private TextView tvTitle;
	private CharSequence title;
	private TextView btnLeft;
	private TextView btnRight;
	private CharSequence rightBtnText;
	
	private View contentView;
	private OnDialogButtonClick listener;
	private final Dialog dialog ;
	private View btnClose;
	private View titleBar;

	public HSIOSDialog(Context context) {
		this.context = context;
		inflater = LayoutInflater.from(context);
		contentView = inflater.inflate(R.layout.view_dialog, null);
		// 新建对话框对象
		 dialog = new AlertDialog.Builder(context).create();
		 dialog.setCancelable(false);
		 dialog.show();
		 findView();
		 dialog.getWindow().setContentView(contentView);
		 //设置监听
		 btnLeft.setOnClickListener(this);
		 btnRight.setOnClickListener(this);
		 btnClose.setOnClickListener(this);
		 
		 message = (String) tvMsg.getText();
		 title = (String) tvTitle.getText();
	}
	
	private void findView(){
		tvMsg = (TextView) contentView.findViewById(R.id.dialog_msg);
		tvTitle = (TextView) contentView.findViewById(R.id.dialog_title);
		btnLeft = (TextView) contentView.findViewById(R.id.dialog_btn_left);
		btnRight = (TextView) contentView.findViewById(R.id.dialog_btn_right);
		titleBar =  contentView.findViewById(R.id.dialog_titlebar);
		btnClose =  contentView.findViewById(R.id.dialog_close_ic);
		
	}

	
	public void show(){
		tvMsg.setText(message);
		tvTitle.setText(title);
		btnRight.setText(rightBtnText);
	}
	
	
	
	/**
	 * 设置msg对齐方式
	 * @param Gravity
	 */
	public void setGravity(int Gravity){
		FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);  
		//此处相当于布局文件中的Android:layout_gravity属性  
		lp.gravity = Gravity;  
		tvMsg.setLayoutParams(lp);
	}
	
	/**
	 * 隐藏显示title
	 * @param visibility View.gone 为隐藏标题栏
	 */
	public void hideTitleBar(){
		titleBar.setVisibility(View.GONE);
		tvMsg.setPadding(0, 120, 0, 120);
	}
	
	/**
	 * 显示dialog上的x图标
	 */
	public void showCloseIcon(){
		btnClose.setVisibility(View.VISIBLE);
		contentView.findViewById(R.id.dialog_title_line).setVisibility(View.GONE);
	}


	/**
	 * 设置对话框界面
	 *           
	 * @return
	 */
	public void setContentView(View v) {
		this.contentView = v;
	}
	
	/**
	 * 设置dialog文本内容
	 * @param messageid 文本资源id
	 * @return
	 */
	public HSIOSDialog setMessage(int messageid) {
		this.message = (String) context.getText(messageid);
		return this;
	}
	/**
	 * 设置dialog文本内容
	 * @param message 
	 * @return
	 */
	public HSIOSDialog setMessage(CharSequence message) {
		this.message = message;
		return this;
	}
	/**
	 * 设置dialog标题文本
	 * @param title 
	 * @return
	 */
	public HSIOSDialog setTitle(CharSequence title) {
		this.title = title;
		return this;
	}
	/**
	 * 设置dialog标题文本
	 * @param titleId 
	 * @return
	 */
	public HSIOSDialog setTitle(int titleId) {
		this.title =  context.getText(titleId);;
		return this;
	}
	/**
	 * 设置右边按钮文字显示
	 * @param rightBtnText
	 * @return
	 */
	public HSIOSDialog setRightBtnText(CharSequence rightBtnText) {
		this.rightBtnText = rightBtnText;
		return this;
	}

	enum DialogStyle {
		ios
	}
	
	public void setBtnClickListener(OnDialogButtonClick listener){
		this.listener = listener;
	}

	@Override
	public void onClick(View v) {
		dialog.dismiss();
		if(listener != null){
			if (v.getId() == R.id.dialog_btn_left) {
				listener.click(LEFTBUTTON);
			}else if(v.getId() == R.id.dialog_btn_right){
				listener.click(RIGHTBUTTON);
			}else if(v.getId() == R.id.dialog_close_ic){
				listener.click(CLOSEBUTTON);
			}
		}
		
	}
	
	
	public interface OnDialogButtonClick{
		public void click(int witch);
	}

}